<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="项目类型" prop="platformType" style="margin-left: -30px;">
        <el-select v-model="queryParams.platformType" placeholder="全部" clearable size="small" style="width: 100%">
          <el-option v-for="item in platformTypeList" :key="item.id" :label="item.title" :value="item.title" />
        </el-select>
      </el-form-item>

      <el-form-item label="关键词查询" prop="keyName">
        <el-input v-model="queryParams.keyName" placeholder="备注" clearable size="small" style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">
          新增
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <el-table :row-class-name="rowStyle" border highlight-current-row v-loading="loading" :data="analyzeList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="项目类型" align="center" prop="platformType" show-overflow-tooltip />
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="是否自动分析" align="center" prop="analyzeStatus" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.analyzeStatus === 1" active-color="#169bd5" inactive-color="#aaaaaa"
            @change="switchShowStatus(scope.row)" />
        </template>
      </el-table-column>


      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:dict:edit']">编辑</el-button>
          <el-button size="mini" style="color: #FA5555;" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <div style='height:80px'></div>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目类型" prop="platformType">
          <el-select v-model="form.platformType" placeholder="请选择项目类型" clearable size="small" style="width: 100%">
            <el-option v-for="item in platformTypeList" :key="item.id" :label="item.title" :value="item.title" />
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 10  }" placeholder="请输入内容">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        
      </div>
    </el-dialog>
    <div class="paginationClass">
      <pagination v-show="total  >  0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { analyze, analyzeAdd, analyzeEdit, analyzeUpdate, analyzeDel, analyzeExport } from "@/api/dictionary/systemSetting/analysis"

export default {
  name: "analysis",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      analyzeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        keyName: undefined,
        platformType: undefined
      },
      platformTypeList: [
        {
          title: '微生物',
          id: 0
        },
        {
          title: '肿瘤',
          id: 1
        },
        {
          title: '科研',
          id: 2
        }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        platformType: [
          { required: true, message: "请选择名称", trigger: "blur" }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询字典类型列表 */
    getList () {
      this.loading = true
      analyze(this.queryParams).then(response => {
        this.analyzeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 字典状态字典翻译
    statusFormat (row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 查询按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = "分析设置：新增"
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const id = row.id || this.ids
      analyzeEdit(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "分析设置：编辑"
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            analyzeUpdate(this.form).then(response => {
              this.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            analyzeAdd(this.form).then(response => {
              this.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const id = row.id || this.ids
      const platformType = row.platformType
      this.$confirm('是否确认删除项目类型为"' + platformType + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return analyzeDel(id)
      }).then(() => {
        this.getList()
        this.msgSuccess("删除成功")
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有类型数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return analyzeExport(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    /** 清理缓存按钮操作 */
    handleClearCache () {
      clearCache().then(response => {
        this.msgSuccess("清理成功")
      })
    },
    //切换状态
    switchShowStatus (row) {
      if (row.analyzeStatus === 1) {
        this.handleUnPublish(row)
      } else if (row.analyzeStatus === 0) {
        this.handlePublish(row)
      }
    },
    handlePublish (row) {
      this.$confirm('是否开启', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return analyzeUpdate({
          ...row,
          analyzeStatus: 1
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("开启成功")
      }).catch(function () {
      })
    },

    handleUnPublish (row) {
      this.$confirm('是否禁用', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return analyzeUpdate({
          ...row,
          analyzeStatus: 0
        })
      }).then(() => {
        this.getList()
        this.msgSuccess("禁用成功")
      }).catch(function () {
      })
    },
    rowStyle ({ row }) {
      var arr = this.ids
      for (let i = 0; i < arr.length; i++) {
        if (row.id === arr[i]) {
          return "rowStyle"
        }
      }
    },
  }
}
</script>
<style scoped>
::v-deep .el-table__body tr.current-row>td {

  background: #a8d6fe !important;
}

::v-deep .rowStyle {
  background-color: #a8d6fe !important;
}

::v-deep .el-table__body tr:hover>td {
  background: #a8d6fe !important;
}

.paginationClass {
  width: 100%;
  height: 70px;
  position: absolute;
  display: flex;
  flex-direction: row-reverse;
  bottom: 0;
  right: 20px;
  z-index: 99;
}
</style>
